package cn.eeepay.framework.service.impl;

import cn.eeepay.framework.dao.*;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.*;
import cn.eeepay.framework.model.allAgent.MerchantAllAgent;
import cn.eeepay.framework.service.*;
import cn.eeepay.framework.service.allAgent.MerchantAllAgentService;
import cn.eeepay.framework.util.*;
import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("merchantBusinessProductService")
@Transactional
public class MerchantBusinessProductServiceImpl implements MerchantBusinessProductService {

    private Logger log = LoggerFactory.getLogger(MerchantBusinessProductServiceImpl.class);

    @Resource
    private MerchantBusinessProductDao merchantBusinessProductDao;

    //商户银行卡
    @Resource
    private MerchantCardInfoService merchantCardInfoService;

    //审核记录
    @Resource
    private ExaminationsLogService examinationsLogService;

    //商户信息
    @Resource
    private MerchantInfoService merchantInfoService;

    //商户服务限额信息
    @Resource
    private MerchantServiceQuotaService merchantServiceQuotaService;

    //商户服务签约费率
    @Resource
    private MerchantServiceRateService merchantServiceRateService;

    //商户进件条件表(明细)
    @Resource
    private MerchantRequireItemService merchantRequireItemService;

    @Resource
    private RouteGroupDao routeGroupDao;

    @Resource
    private UserService userService;

    @Resource
    private PosCardBinService posCardBinService;

    @Resource
    private BusinessProductInfoDao businessProductInfoDao;

    @Resource
    private AgentInfoService agentInfoService;

    @Resource
    private SysDictService sysDictService;
    @Resource
    private BossSysConfigService bossSysConfigService;

    @Resource
    private AreaInfoService areaInfoService;

    @Resource
    private MerchantRequireHistoryService merchantRequireHistoryService;

    @Resource
    private SysDictDao sysDictDao;

    @Resource
    private SensitiveWordsService sensitiveWordsService;

    @Resource
    private ZqMerchantInfoDao zqMerchantInfoDao;

    @Resource
    private MerchantAllAgentService merchantAllAgentService;
    @Resource
    private ChangeLogService changeLogService;

    @Resource
    private TeamInfoDao teamInfoDao;

    @Resource
    private YfbOemServiceDao yfbOemServiceDao;

    @Resource
    private UserEntityDao userEntityDao;

    @Resource
    private MerchantInfoDao merchantInfoDao;

    @Resource
    private UserDao userDao;

    @Resource
    private AgentInfoDao agentInfoDao;

    @Override
    public MerchantBusinessProduct selectByPrimaryKey(Long id) {
        return merchantBusinessProductDao.selectByPrimaryKey(id);
    }


    @Override
    public List<MerchantBusinessProduct> selectAllByStatusInfo(Page<MerchantBusinessProduct> page) {
        return merchantBusinessProductDao.selectAllByStatusInfo(page);
    }

    @Override
    public List<MerchantBusinessProduct> selectMerBusProByIds(List<String> ids) {
        return merchantBusinessProductDao.selectMerBusProByIds(ids);
    }

    @Override
    public int updateStatusById(String status, String id) {
        return merchantBusinessProductDao.updateStatusById(status, id);
    }

    @Override
    public List<MerchantBusinessProduct> selectByParam(Page<MerchantBusinessProduct> page, MbpSelectParams mbpSelectParams) {
        if (mbpSelectParams.getProvince() != null && !"".equals(mbpSelectParams.getProvince())) {
            Map<String, Object> map = areaInfoService.getProvincebyId(Integer.valueOf(mbpSelectParams.getProvince()));
            mbpSelectParams.setProvince(map.get("name").toString());
        }
        merchantBusinessProductDao.selectByParam(page, mbpSelectParams);
        List<MerchantBusinessProduct> list = page.getResult();
        getTeamName(list);
        page.setResult(list);
        return list;
    }

    /**
     * 获取商户的组织名称
     *
     * @param list
     */
    private void getTeamName(List<MerchantBusinessProduct> list) {
        //商户的组织编号是根据oem来的，对这些商户的组织名称进行处理
        //本次处理6开头的，比如超级还600010、超级银行家610010
        //数据字典配置60开头是超级还，61开头是超级银行家 71开头是超级兑分销版
        if (list == null || list.size() < 1) {
            return;
        }
        for (MerchantBusinessProduct merchant : list) {
            if (merchant.getTeamId() != null && merchant.getTeamId().length() > 2) {
                if (merchant.getTeamId().startsWith("6") || merchant.getTeamId().startsWith("71")) {
                    List<Map<String, Object>> teamInfoList = merchantBusinessProductDao.getTeamInfo(merchant.getTeamId().substring(0, 2));
                    if (teamInfoList != null && teamInfoList.size() > 0) {
                        merchant.setTeamName(teamInfoList.get(0).get("team_name").toString());
                    }
                }
            }
        }
    }

    @Override
    public int updateBymbpId(Long mbpId, String status, String auditor) {
        return merchantBusinessProductDao.updateBymbpId(mbpId, status, auditor);
    }

    @Override
    public int reexamineBymbpId(Long mbpId, String status, String auditor) {
        return merchantBusinessProductDao.reexamineBymbpId(mbpId, status, auditor);
    }

    @Override
    public int resetReexamineBymbpId(Long mbpId, String status, Date time) {
        return merchantBusinessProductDao.resetReexamineBymbpId(mbpId, status, time);
    }

    @Override
    public Map<String, Object> updateByItemAndMbpId(MerchantBusinessProduct merchantBusinessProduct,
                                                    MerchantInfo merchantInfo, List<MerchantRequireItem> merchantRequireItemList,
                                                    List<MerchantServiceRate> merchantServiceRateList, List<MerchantServiceQuota> merchantServiceQuotaList, Integer userId, UserModel userModel) {
        int i = 0;
        Map<String, Object> jsonMap = new HashMap<>();
        //校验商户名称和商户经营地址，是否包含敏感词
        if (sensitiveWordsService.selectContain(merchantInfo.getMerchantName())) {
            throw new RuntimeException("修改失败，商户名称包含敏感词");
        }
        i = merchantBusinessProductDao.updateByItemAndMbpId(merchantBusinessProduct);
        if (i != 1) {
            throw new RuntimeException("商户业务产品信息修改失败");
        }

        int nu = 0;
        String logAccount = "";
        String account = "";
        for (MerchantRequireItem merchantRequireItem : merchantRequireItemList) {
            if (merchantRequireItem.getMriId().equals("3")) {
                logAccount = merchantRequireItem.getLogContent();
                account = merchantRequireItem.getContent();
                break;
            }
        }
        MerchantCardInfo merchantCardInfo = new MerchantCardInfo();
        merchantCardInfo.setMerchantNo(merchantInfo.getMerchantNo());
        merchantCardInfo.setLogAccountNo(logAccount);
        MerchantCardInfo mci = merchantCardInfoService.selectByMertIdAndAccountNo(merchantCardInfo);
        if (mci == null) {
            throw new RuntimeException("商户开户账号和默认结算卡不匹配");
        }
        if (!logAccount.equals(account)) {//新旧结算卡不一致，修改过结算卡
            jsonMap.put("changeSettleCard", 1);
        }
        String idCardNo = "";
        String settleCardNo = "";
        String coreUrl = sysDictService.getValueByKey("CORE_URL");
        String risk130Key = bossSysConfigService.selectValueByKey("RISK130_KEY");
        String oldProvince = null;//旧的省市区
        String oldAddress = null;//旧的经营地址
        for (MerchantRequireItem merchantRequireItem : merchantRequireItemList) {
            if (merchantRequireItem.getMriId().equals("37")) {//临时解决方法经营范围不作修改
                continue;
            }

            if (!merchantBusinessProduct.getStatus().equals("1") && !merchantBusinessProduct.getStatus().equals("2") && !merchantBusinessProduct.getStatus().equals("0")) {
                //开户身份证
                if (merchantRequireItem.getMriId().equals("6") && merchantRequireItem.getStatus().equals("1")) {
                    if (checkIcCardNoTitle(merchantRequireItem.getContent())) {
                        merchantInfo.setIdCardNo(merchantRequireItem.getContent());
                    } else {
                        throw new RuntimeException("暂不支持进件!");
                    }
                }

                //开户行全称
                if (merchantRequireItem.getMriId().equals("4") && merchantRequireItem.getStatus().equals("1")) {
                    mci.setBankName(merchantRequireItem.getContent());
                    userModel.setBankName(merchantRequireItem.getContent());
                }
                //卡号
                if (merchantRequireItem.getMriId().equals("3") && merchantRequireItem.getStatus().equals("1")) {
                    settleCardNo = merchantRequireItem.getContent();
                    mci.setAccountNo(merchantRequireItem.getContent());
                }
                //开户名
                if (merchantRequireItem.getMriId().equals("2") && merchantRequireItem.getStatus().equals("1")) {
                    mci.setAccountName(merchantRequireItem.getContent());
                }
                //账户类型
                if (merchantRequireItem.getMriId().equals("1") && merchantRequireItem.getStatus().equals("1")) {
                    if (merchantRequireItem.getContent().equals("对公")) {
                        mci.setAccountType("1");
                    } else {
                        mci.setAccountType("2");
                    }
                }
                //联行行号
                if (merchantRequireItem.getMriId().equals("5") && merchantRequireItem.getStatus().equals("1"))
                    mci.setCnapsNo(merchantRequireItem.getContent());
                //经营地址拆分
                if (merchantRequireItem.getMriId().equals("7") && merchantRequireItem.getStatus().equals("1")) {
                    String content = merchantRequireItem.getContent();
                    String[] str = content.split("-");
                    if (str.length <= 3) {
                        throw new RuntimeException("经营地址有误");
                    }
                    if (sensitiveWordsService.selectContain(str[3])) {
                        throw new RuntimeException("修改失败，商户经营详细地址包含敏感词");
                    }
                    nu = 1;

                    oldProvince = merchantInfo.getProvince() + merchantInfo.getCity() + merchantInfo.getDistrict();
                    oldAddress = merchantInfo.getAddress();


                    merchantInfo.setProvince(str[0]);
                    merchantInfo.setCity(str[1]);
                    merchantInfo.setDistrict(str[2]);

                    merchantInfo.setAddress(str[0] + str[1] + str[2] + str[3]);

                    /*MerchantRequireItem oldMri = merchantRequireItemService.selectByMriId(merchantRequireItem.getMriId(), merchantRequireItem.getMerchantNo());
                    if (oldMri != null) {
                        oldAddress = oldMri.getContent();
                    }*/

                }
                //开户地址拆分
                if (merchantRequireItem.getMriId().equals("15") && merchantRequireItem.getStatus().equals("1")) {
                    String content = merchantRequireItem.getContent();
                    String[] str = content.split("-");
                    if (str.length <= 2) {
                        throw new RuntimeException("开户地址有误");
                    }
                    mci.setAccountProvince(str[0]);
                    mci.setAccountCity(str[1]);
                    mci.setAccountDistrict(str[2]);
                }
            }

            if (merchantRequireItem.getExampleType().equals("1"))
                continue;
            MerchantRequireItem mrit = merchantRequireItemService.selectByMriId(merchantRequireItem.getMriId(), merchantRequireItem.getMerchantNo());

            if (merchantRequireItem.getMriId().equals("6")) {
                idCardNo = merchantRequireItem.getContent();
            }
            if (!mrit.getContent().equals(merchantRequireItem.getContent())) {
                //风控规则130调用
                if (merchantRequireItem.getMriId().equals("3") || merchantRequireItem.getMriId().equals("6")) {
                    String responseMsg = ClientInterface.risk130(coreUrl, idCardNo, settleCardNo, risk130Key);
                    if (StringUtil.isNotBlank(responseMsg)) {
                        Map<String, Object> responseMap = JSON.parseObject(responseMsg).getJSONObject("header");
                        Boolean succeed = (Boolean) responseMap.get("succeed");
                        if (!succeed) {
                            String msg = (String) responseMap.get("errMsg");
                            throw new RuntimeException(msg);
                        }
                    }
                }
                i = merchantRequireItemService.updateByMbpId(merchantRequireItem);
                if (i != 1) {
                    throw new RuntimeException("商户进件项修改失败");
                }
            }
        }

        if (!merchantBusinessProduct.getStatus().equals("1") && !merchantBusinessProduct.getStatus().equals("2") && !merchantBusinessProduct.getStatus().equals("0")) {
            if (merchantCardInfoService.selectByMertIdAndAccountNo(merchantCardInfo) != null) {
                PosCardBin pdd = posCardBinService.queryInfo(mci.getAccountNo());
                mci.setLogAccountNo(logAccount);
                if (pdd != null) {
                    if (pdd.getCardType().equals("借记卡")) {
                        mci.setCardType("1");
                    } else {
                        mci.setCardType("2");
                    }
                    i = merchantCardInfoService.updateByMerId(mci);
                    if (i != 1) {
                        throw new RuntimeException("商户账号信息修改失败");
                    }

                    //结算卡号不一样才插入日志
                    if (!mci.getAccountNo().equals(mci.getLogAccountNo())) {
                        MerchantRequireHistory mer = new MerchantRequireHistory();
                        mer.setMerchantNo(mci.getMerchantNo());
                        mer.setHistoryContent(mci.getLogAccountNo());
                        mer.setNewContent(mci.getAccountNo());
                        mer.setCreator(userId + "");
                        int num = merchantRequireHistoryService.insertBankCard(mer);
                        if (num < 0) {
                            throw new RuntimeException("商户账号信息修改日志记录失败");
                        }
                    }


                } else {
                    throw new RuntimeException("银行账号匹配不上");
                }
            }
            if (nu == 1) {
                i = merchantInfoService.updateAddressByMerId(merchantInfo);
                if (i != 1) {
                    throw new RuntimeException("商户地址信息修改失败");
                }

                //新旧经营地址不一致，插入日志记录
                if (oldAddress != null && !oldAddress.equals(merchantInfo.getAddress())) {
                    MerchantRequireHistory mer = new MerchantRequireHistory();
                    mer.setMriId("7");
                    mer.setMerchantNo(mci.getMerchantNo());
                    mer.setHistoryContent(oldAddress);
                    mer.setNewContent(merchantInfo.getAddress());
                    mer.setCreator(userId + "");
                    mer.setRemark("BOSS修改商户经营地址");
                    int num = merchantRequireHistoryService.insertMerchantRequireHistory(mer);
                    if (num < 0) {
                        throw new RuntimeException("商户经营地址信息修改日志记录失败");
                    }


                    //经营地址改变调用CORE进行集群切换
                    if (!oldProvince.equals(merchantInfo.getProvince() + merchantInfo.getCity() + merchantInfo.getDistrict())) {

                        jsonMap.put("needSynMerAddress", "1");

                        //搞个标识，最后面才去调用core，事务中。

                    }


                }

            }
        }
        //用商户编号查询这个商户的手机号（旧），与现在的手机号做比较
        MerchantInfo misd1 = merchantInfoService.selectByMobilephoneAndTeam(merchantInfo.getMobilephone(),
                merchantInfo.getTeamId());
        MerchantInfo misd = merchantInfoService.selectByMerNo(merchantBusinessProduct.getMerchantNo());
        if (misd1 != null) {
            if (!misd1.getMerchantNo().equals(merchantInfo.getMerchantNo())) {
                throw new RuntimeException("手机号已存在，请重试");
            }
        } else {
            UserInfo uis = userService.selectInfoByTelNo(misd.getMobilephone(), misd.getTeamId());
            if (uis == null) {
                throw new RuntimeException("手机号不存在，请重试");
            } else {
                int ppm = userService.updateInfoByMp(misd.getMobilephone(), merchantInfo.getMobilephone(), misd.getTeamId());
                if (ppm < 1) {
                    throw new RuntimeException("手机号修改失败，请重试");
                } else {
                    jsonMap.put("mobile", "yes");
                }
            }
        }
        if (merchantInfo.getRemark() == null) {
            merchantInfo.setRemark("");
        }
        if (misd.getRemark() == null) {
            misd.setRemark("");
        }
        if (!misd.getMobilephone().equals(merchantInfo.getMobilephone()) || !misd.getMerchantName().equals(merchantInfo.getMerchantName())
                || !merchantInfo.getRemark().equals(misd.getRemark()) || !merchantInfo.getLawyer().equals(misd.getLawyer())
                || !merchantInfo.getStatus().equals(misd.getStatus()) || !merchantInfo.getIdCardNo().equals(misd.getIdCardNo())) {
            i = merchantInfoService.updateByMerId(merchantInfo);
            //判断是不是超级盟主的商户
            MerchantAllAgent merchantAllAgent = merchantAllAgentService.queryMerchantAllAgentByMerNo(merchantInfo.getMerchantNo());
            if (merchantAllAgent != null) {
                ChangeLog changeLog = new ChangeLog();
                final UserLoginInfo principal = (UserLoginInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                changeLog.setOperater(principal.getId().toString());
                changeLog.setOperMethod("updateByItemAndMbpId");
                changeLog.setRemark("修改超级盟主商户");
                changeLog.setChangePre(JSONObject.toJSONString(merchantAllAgent));
                merchantAllAgent.setStatus(merchantInfo.getStatus());
                merchantAllAgent.setMobilePhone(merchantInfo.getMobilephone());
                merchantAllAgentService.updateMerchantAllAgentByMerNo(merchantAllAgent);
                changeLog.setChangeAfter(JSONObject.toJSONString(merchantAllAgent));
                changeLogService.insertChangeLog(changeLog);
                log.info("修改超级盟主商户成功...........");
            }
            if (i != 1) {
                throw new RuntimeException("商户信息修改失败");
            }
        }

        for (MerchantServiceRate merchantServiceRate : merchantServiceRateList) {
            if (merchantServiceRate.getFixedMark().equals("0")) {
                MerchantServiceRate m = new MerchantServiceRate();
                m = merchantServiceRateService.setMerchantServiceRate(merchantServiceRate);
                m.setId(merchantServiceRate.getId());
                m.setServiceId(merchantServiceRate.getServiceId());
                m.setMerchantNo(merchantServiceRate.getMerchantNo());
                m.setHolidaysMark(merchantServiceRate.getHolidaysMark());
                m.setCardType(merchantServiceRate.getCardType());
                m.setRateType(merchantServiceRate.getRateType());
                i = merchantServiceRateService.updateByPrimaryKey(m);
                if (i != 1) {
                    throw new RuntimeException("商户费率改失败");
                }
            }
        }

        for (MerchantServiceQuota merchantServiceQuota : merchantServiceQuotaList) {
            if (merchantServiceQuota.getFixedMark().equals("0")) {
                i = merchantServiceQuotaService.updateByPrimaryKey(merchantServiceQuota);
                if (i != 1) {
                    throw new RuntimeException("商户限额修改失败");
                }
            }
        }

        // 修改商户手机号时调用积分系统修改手机号
        String vipScoreUrl = sysDictService.getValueByKey("VIP_SCORE_URL");
        String teamId = misd.getTeamId();
        String key = bossSysConfigService.selectValueByKey("VIP_SCORE_SIGN_KEY_" + teamId);
        String businessNo = bossSysConfigService.selectValueByKey("VIP_SCORE_BUS_NO_" + teamId);
        merchantInfo.setUserId(userEntityDao.getUserIdByMerchantNo(merchantInfo.getMerchantNo()));
        String resultMsg = ClientInterface.updateVipInfo(vipScoreUrl, merchantInfo, misd.getMobilephone(), businessNo, teamId, key);

        jsonMap.put("result", true);
        jsonMap.put("msg", "修改成功");
        return jsonMap;
    }

    /**
     * 校验身份证号是否为禁用开头
     *
     * @param idCardNo 身份证号
     * @return false 表示禁用，true表示可以用
     */
    private boolean checkIcCardNoTitle(String idCardNo) {
        //多个,号间隔
        String checkStr = sysDictDao.getValueByKey("ID_CARD_PREFIX_OF_FORBID_ADD_MERCHANT");
        if (StringUtils.isNotBlank(checkStr) && idCardNo.length() >= 6) {
            String str = "," + idCardNo.substring(0, 6) + ",";
            String strs = "," + checkStr + ",";
            //如果过滤字符包含 身份证前6位，则返回false
            if (strs.indexOf(str) >= 0) {
                return false;
            }
        }
        return true;
    }

    public Result ysUpdateMer(String merchantNo, String mbpId, String bpId, String acqEnname) {
        Result result = new Result();
        result.setMsg("修改成功");
        ZqMerchantInfo zqMerchantInfo = zqMerchantInfoDao.selectByMerMbpAcq(merchantNo, mbpId, acqEnname);
        if (zqMerchantInfo != null && "1".equals(zqMerchantInfo.getReportStatus())) {
            String coreUrl = "";
            SysDict coreUrlDict = sysDictDao.getByKey("CORE_URL");
            if (coreUrlDict != null) {
                coreUrl = coreUrlDict.getSysValue() + Constants.YS_UPDATE_MERCHANT_INFO;
            }
            try {
                String returnStr = ClientInterface.ysUpdateMerInfo(coreUrl, merchantNo, mbpId, bpId);
                if (StringUtils.isNotBlank(returnStr)) {
                    JSONObject json = JSONObject.parseObject(returnStr);
                    JSONObject headJson = JSONObject.parseObject(json.getString("header"));
                    if (headJson.getBoolean("succeed")) {
                        result.setStatus(true);
                        result.setMsg("操作成功，且同步商户成功");
                    } else {
                        result.setMsg("操作成功，且同步商户失败，失败原因:" + headJson.getString("errMsg"));
                    }
                } else {
                    result.setMsg("操作成功，同步商户失败");
                }
            } catch (Exception e) {
                log.info("商户修改，同步银盛失败", e);
            }
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> examineTotalByParam(MbpSelectParams mbpSelectParams) {
        if (mbpSelectParams.getProvince() != null && !"".equals(mbpSelectParams.getProvince())) {
            Map<String, Object> map = areaInfoService.getProvincebyId(Integer.valueOf(mbpSelectParams.getProvince()));
            mbpSelectParams.setProvince(map.get("name").toString());
        }
        return merchantBusinessProductDao.examineTotalByParam(mbpSelectParams);
    }

    @Override
    public void exportExamine(MbpSelectParams mbpSelectParams, HttpServletResponse response, HttpServletRequest request) {
        log.info("开始执行导出");
        if (mbpSelectParams.getProvince() != null && !"".equals(mbpSelectParams.getProvince())) {
            Map<String, Object> map = areaInfoService.getProvincebyId(Integer.valueOf(mbpSelectParams.getProvince()));
            mbpSelectParams.setProvince(map.get("name").toString());
        }
        List<MerchantBusinessProduct> list = merchantBusinessProductDao.exportExamine(mbpSelectParams);
        log.info("准备执行导出数据条数：{}", list.size());
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> syncStatusMap = new HashMap<>();
        syncStatusMap.put("0", "初始化");
        syncStatusMap.put("1", "同步成功");
        syncStatusMap.put("2", "同步失败");
        syncStatusMap.put("3", "审核中");
        Map<String, String> statusMap = new HashMap<>();
        statusMap.put("1", "待一审");
        statusMap.put("2", "待平台审核");
        statusMap.put("3", "审核失败");
        statusMap.put("4", "正常");
        Map<String, String> riskStatus = new HashMap<>();
        riskStatus.put("1", "正常");
        riskStatus.put("2", "只进不出");
        riskStatus.put("3", "不进不出");
        Map<String, String> recommendedSource = sysDictService.selectMapByKey("RECOMMENDED_SOURCES");
//		recommendedSource.put("0","正常注册");
//		recommendedSource.put("1","微创业");
//		recommendedSource.put("2","代理商分");
//		recommendedSource.put("3","超级盟主");
        Map<String, String> reexamineStatus = new HashMap<>();
        reexamineStatus.put("0", "未复审");
        reexamineStatus.put("1", "复审通过");
        reexamineStatus.put("2", "复审不通过");
        reexamineStatus.put("3", "复审退件");
        List<SysDict> sysDictList = sysDictService.selectByKey("MERCHANT_TYPE_LIST");
        String fileName = "复审商户导出" + sdf.format(new Date()) + ".xlsx";
        String fileNameFormat = null;
        try {
            fileNameFormat = new String(fileName.getBytes("GBK"), "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileNameFormat);
        log.info("开始exl数据组装");
        for (MerchantBusinessProduct mbp : list) {

            //敏感信息屏蔽
            mbp.setMobilePhone(StringUtil.sensitiveInformationHandle(mbp.getMobilePhone(), 0));

            Map<String, String> maps = new HashMap<String, String>();
            maps.put("id", mbp.getId().toString());
            maps.put("teamName", mbp.getTeamName());//displayName:'所属组织',width:150
            maps.put("merchantNo", mbp.getMerchantNo());
            maps.put("merchantType", mbp.getMerchantType() == null ? "" : SysDictUtil.getSysNameByValue(sysDictList, mbp.getMerchantType()));
            maps.put("tradeType", "1".equals(mbp.getTradeType()) ? "直清模式" : "集群模式");
            maps.put("merchantName", mbp.getMerchantName());
            maps.put("mobilePhone", mbp.getMobilePhone());
            maps.put("bpName", mbp.getBpName());
            maps.put("agentName", mbp.getAgentName());
            maps.put("activityCode", "1".equals(mbp.getActivityCode()) ? "是" : "否");
            maps.put("merAccount", "1".equals(mbp.getMerAccount()) ? "是" : "否");
            maps.put("syncStatus", syncStatusMap.get(mbp.getSyncStatus()));
            maps.put("controlAmount", StringUtil.filterNull(mbp.getControlAmount()));
            maps.put("preFrozenAmount", StringUtil.filterNull(mbp.getPreFrozenAmount()));
            maps.put("merCreateTime", sdf.format(mbp.getMerCreateTime()));
            maps.put("reexamineTime", mbp.getReexamineTime() == null ? "" : sdf.format(mbp.getReexamineTime()));
            maps.put("merStatus", "1".equals(mbp.getMerStatus()) ? "正常" : "关闭");
            maps.put("reexamineStatus", reexamineStatus.get(mbp.getReexamineStatus()));
            maps.put("reexamineOperator", mbp.getReexamineOperator());
            maps.put("status", statusMap.get(mbp.getStatus()));
            maps.put("riskStatus", riskStatus.get(mbp.getRiskStatus()));
            maps.put("recommendedSource", recommendedSource.get(mbp.getRecommendedSource()));
            data.add(maps);
        }
        log.info("exl数据组装完成");
        ListDataExcelExport export = new ListDataExcelExport();
        String[] cols = new String[]{"id", "teamName", "merchantNo", "merchantType", "tradeType", "merchantName", "mobilePhone", "bpName", "agentName", "activityCode",
                "merAccount", "syncStatus", "controlAmount", "preFrozenAmount", "merCreateTime", "reexamineTime", "merStatus", "reexamineStatus", "reexamineOperator",
                "status", "riskStatus", "recommendedSource"};
        String[] colsName = new String[]{"商户进件编号", "所属组织", "商户编号", "商户类型", "交易模式", "商户名称", "商户手机号", "业务产品", "代理商名称", "是否参加欢乐送",
                "已开户", "直清同步状态", "冻结金额", "预冻结金额", "创建时间", "复审时间", "商户状态", "复审状态", "复审人",
                "业务产品状态", "商户冻结状态", "推广来源"};

        OutputStream outputStream = null;
        log.info("exl数据导出");
        try {
            outputStream = response.getOutputStream();
            export.export(cols, colsName, data, outputStream);
        } catch (IOException e) {
            log.error("导出异常", e);
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String getTeamName(String teamId) {
        String teamName = "";
        if (StringUtils.isBlank(teamId)) {
            return teamName;
        }
        if (!StringUtil.isNumeric(teamId)) {
            return teamName;
        }
        Integer teamIdInt = Integer.parseInt(teamId);
        TeamInfo teamInfo = teamInfoDao.selectTeamInfo(teamIdInt);
        if (teamInfo != null) {
            teamName = teamInfo.getTeamName();
        } else {
            teamName = yfbOemServiceDao.selectTeamName(teamId);
        }
        return teamName;
    }

    @Override
    public String getCoreUrl() {
        String coreUrl = "";
        SysDict coreUrlDict = sysDictDao.getByKey("CORE_URL");
        if (coreUrlDict != null) {
            coreUrl = coreUrlDict.getSysValue();
        }
        return coreUrl;
    }


    @Override
    public int updateMbpExamineInfo(MbpStatusCondition mbpStatus, MerchantBusinessProduct mbpInfo, List<MerchantRequireItem> mri, MerchantInfo merchantInfo, MerchantCardInfo mci, ExaminationsLog el) {
        int val = mbpStatus.getVal();
        //银行卡状态
        boolean bankCardStatus = mbpStatus.getBankCardStatus();
        //开户行全称，开户地址
        boolean addressStatus = mbpStatus.getBankCardStatus();

        boolean reexamineStatus = mbpStatus.getReexamineStatus();

        boolean appStatus = mbpStatus.getAppStatus();

        int num = 0;
        if (val == 1 || val == 3) {//审核成功
            el.setOpenStatus("1");
            //更新进件表
            num = updateBymbpId(mbpInfo.getId(), "4", el.getOperator());
            if (num > 0) {
                //更新子进件项
                updateMerchantRequireItem(val, mri, bankCardStatus, addressStatus);

                //审核过,增加初始统计次数
                merchantBusinessProductDao.updateMbpAuditNum(mbpInfo.getId());

                //商户信息
                merchantInfoService.updateByPrimaryKey(merchantInfo);

                //商户银行卡信息merchant_id
                mci.setDefSettleCard("1");
                if (merchantCardInfoService.selectByMertIdAndAccountNo(mci) == null) {
                    merchantCardInfoService.insert(mci);
                } else {
                    merchantCardInfoService.updateById(mci);
                }

                //路由
                routeGroupDao.insertRouteGroupByMerchant(merchantInfo.getMerchantNo());

                //审核日志
                //校验商户是否有过复审退件再次进件
                if ("3".equals(mbpInfo.getReexamineStatus())) {
                    //被复审退件的商户
                    el.setExamineType(2);
                    int count = examinationsLogService.insert(el);

                    //商户曾被复审退件，再次进件，审核成功，其余件全部成功
                    List<MerchantBusinessProduct> mbpList = getByMer(merchantInfo.getMerchantNo());
                    for (MerchantBusinessProduct mbp : mbpList) {
                        Long otherMbpId = mbp.getId();
                        if (!mbpInfo.getId().toString().equals(otherMbpId.toString())) {
                            updateBymbpId(otherMbpId, "4", el.getOperator());
                            el.setBpId(mbp.getBpId());
                            el.setItemNo(String.valueOf(otherMbpId));
                            el.setExamineType(2);
                            examinationsLogService.insert(el);
                        }
                    }
                } else {
                    int count = examinationsLogService.insert(el);
                    if (count > 0) {//增加审核记录统计数据
                        examinationsLogService.insertLogExt(el);
                    }
                }
            }


        } else if (val == 2 || val == 4) {//不通过
            el.setOpenStatus("2");
            if (!reexamineStatus) {
                //正常审件
                num = updateBymbpId(mbpInfo.getId(), "3", el.getOperator());
                if (num > 0) {
                    //更新子进件项
                    updateMerchantRequireItem(val, mri, bankCardStatus, addressStatus);

                    //开户名,银行卡,开户身份证,更新APP自动审件次数为3
                    if (appStatus) {
                        updateCheckNum(merchantInfo.getMobilephone(), merchantInfo.getTeamId());
                    }

                    //审核过,增加初始统计次数
                    merchantBusinessProductDao.updateMbpAuditNum(mbpInfo.getId());

                    if ("3".equals(mbpInfo.getReexamineStatus())) {
                        //被复审退件的商户
                        el.setExamineType(2);
                        examinationsLogService.insert(el);

                        //属风控复审退件，继续退其他进件...
                        List<MerchantBusinessProduct> mbpList = getByMer(merchantInfo.getMerchantNo());
                        for (MerchantBusinessProduct mbp : mbpList) {
                            Long otherMbpId = mbp.getId();
                            if (!mbpInfo.getId().toString().equals(otherMbpId.toString())) {
                                updateBymbpId(otherMbpId, "3", el.getOperator());
                                el.setExamineType(2);
                                el.setBpId(mbp.getBpId());
                                el.setItemNo(String.valueOf(otherMbpId));
                                el.setOpenStatus("2");
                                examinationsLogService.insert(el);
                            }
                        }
                    } else {
                        int count = examinationsLogService.insert(el);
                        if (count > 0) {//增加审核记录统计数据
                            examinationsLogService.insertLogExt(el);
                        }
                    }
                }

            } else {
                //复审退件
                num = updateBymbpId(mbpInfo.getId(), "3", el.getOperator());
                if (num > 0) {

                    //更新子进件项
                    updateMerchantRequireItem(val, mri, bankCardStatus, addressStatus);

                    //开户名,银行卡,开户身份证,更新APP自动审件次数为3
                    if (appStatus) {
                        updateCheckNum(merchantInfo.getMobilephone(), merchantInfo.getTeamId());
                    }

                    //审核日志
                    el.setOpenStatus("2");
                    examinationsLogService.insert(el);

                    //复审退件,获取当前商户其余进件，全部退件
                    List<MerchantBusinessProduct> mbpList = getByMer(merchantInfo.getMerchantNo());
                    final UserLoginInfo principal = (UserLoginInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                    for (MerchantBusinessProduct mbp : mbpList) {
                        Long otherMbpId = mbp.getId();
                        if (!mbpInfo.getId().toString().equals(otherMbpId.toString())) {
                            updateBymbpId(otherMbpId, "3", el.getOperator());
                            reexamineBymbpId(otherMbpId, "3", principal.getUsername());
                            el.setBpId(mbp.getBpId());
                            el.setItemNo(String.valueOf(otherMbpId));
                            el.setExamineType(2);
                            el.setOpenStatus("3");
                            examinationsLogService.insert(el);
                        }
                    }
                }
            }
        }
        return num;
    }


    /**
     * 更新子进件项
     *
     * @param val
     * @param mri
     * @param bankCardStatus 银行卡状态
     * @param addressStatus  开户行全称，开户地址
     * @return
     */
    private void updateMerchantRequireItem(int val, List<MerchantRequireItem> mri, boolean bankCardStatus, boolean addressStatus) {
        if (mri != null && mri.size() > 0) {
            for (MerchantRequireItem item : mri) {
                if (val == 1 || val == 3) {
                    merchantRequireItemService.updateBymriId(item.getId(), "1");
                } else if (val == 2 || val == 4) {

                    if ("不通过".equals(item.getaStatus())) {
                        merchantRequireItemService.updateBymriId(item.getId(), "2");
                    } else {
                        if (bankCardStatus && ("4".equals(item.getMriId()) || "5".equals(item.getMriId()) || "15".equals(item.getMriId()))) {
                            merchantRequireItemService.updateBymriId(item.getId(), "2");
                            continue;
                        }
                        if (addressStatus && ("4".equals(item.getMriId()) || "15".equals(item.getMriId()))) {
                            merchantRequireItemService.updateBymriId(item.getId(), "2");
                            continue;
                        }
                        merchantRequireItemService.updateBymriId(item.getId(), "1");
                    }
                }
            }
        }
    }

    @Override
    public List<MerchantBusinessProduct> selectAllInfo(Page<MerchantBusinessProduct> page) {
        return merchantBusinessProductDao.selectAllInfo(page);
    }

    @Override
    public List<String> querySerivceId(String bpId) {
        return businessProductInfoDao.findByProduct(bpId);
    }

    @Override
    public List<MerchantBusinessProduct> selectByStatusParam(Page<MerchantBusinessProduct> page,
                                                             MbpSelectParams mbpSelectParams) {
        List<MerchantBusinessProduct> list = merchantBusinessProductDao.selectByStatusParam(page, mbpSelectParams);
        getTeamName(page.getResult());
        return list;
    }

    @Override
    public List<MerchantBusinessProduct> selectAllInfoSale(Page<MerchantBusinessProduct> page, String name) {
        List<String> list = agentInfoService.selectAllNodeSale(name);
        if (list.size() == 0) {
            return null;
        }
        String str = "";
        for (String agentInfo : list) {
            str += " or ais.agent_node LIKE " + '"' + agentInfo + '"';
        }
        return merchantBusinessProductDao.selectAllInfoSale(page, str.substring(3, str.length()));
    }

    @Override
    public List<MerchantBusinessProduct> selectByParamSale(Page<MerchantBusinessProduct> page,
                                                           MbpSelectParams mbpSelectParams, String name) {
        List<String> list = agentInfoService.queryAllOneAgentBySale(name);
        if (list.size() == 0) {
            return null;
        }
        //xy328tgh
//		StringBuilder str=new StringBuilder("(");
//		for (String agentNo : list) {
//			str.append("'").append(agentNo).append("'").append(",");
//		}
//		str.deleteCharAt(str.length()-1);
//		str.append(")");
        mbpSelectParams.setSaleName(name);
        return merchantBusinessProductDao.selectByParamSale(page, mbpSelectParams);
    }

    @Override
    public List<AutoCheckResult> selectAutoCheckResult(String merchantNo, String bpId) {
        return merchantBusinessProductDao.selectAutoCheckResult(merchantNo, bpId);
    }

    @Override
    public int updateCheckNum(String mobile, String teamId) {
        return merchantBusinessProductDao.updateCheckNum(mobile, teamId);
    }

    @Override
    public MerchantBusinessProduct selectMerBusPro(String merchantNo, String bpId) {
        return merchantBusinessProductDao.selectMerBusPro(merchantNo, bpId);
    }

    @Override
    public Integer selectMerProLimit(String merchantNo) {
        return merchantBusinessProductDao.selectMerProLimit(merchantNo);
    }

    @Override
    public MerchantBusinessProduct findCollectionCodeMbp(String merchantNo) {
        return merchantBusinessProductDao.findCollectionCodeMbp(merchantNo);
    }

    @Override
    public List<BusinessProductDefine> selectSourceBpInfo() {
        return merchantBusinessProductDao.selectSourceBpInfo();
    }

    @Override
    public List<BusinessProductDefine> selectNewBpInfo() {
        return merchantBusinessProductDao.selectNewBpInfo();
    }

    @Override
    public Page<MerchantBusinessProductHistory> selectMerBpHistoryList(Map<String, Object> params,
                                                                       Page<MerchantBusinessProductHistory> page) {
        merchantBusinessProductDao.selectMerBpHistoryList(params, page);
        return page;
    }

    @Override
    public List<MerchantBusinessProduct> getByMer(String merchantNo) {
        return merchantBusinessProductDao.getByMer(merchantNo);
    }

    @Override
    public int updateTradeTypeById(Long primaryKey, String tradeType) {
        return merchantBusinessProductDao.updateTradeTypeById(primaryKey, tradeType);
    }

    @Override
    public MerchantBusinessProduct selectByServiceId(Long merServiceId) {
        return merchantBusinessProductDao.selectByServiceId(merServiceId);
    }

    @Override
    public int updateTradeTypeByServices(MerchantBusinessProduct merBusPro) {
        return merchantBusinessProductDao.updateTradeTypeByServices(merBusPro);
    }

    @Override
    public String queryExamineStatus(String bpId, String merchantNo) {
        return merchantBusinessProductDao.queryExamineStatus(bpId, merchantNo);
    }

    @Override
    public List<Map<String, Object>> findDefRouteGroupAdd(String merchantNo, String bpId, String channelCode) {
        return merchantBusinessProductDao.findDefRouteGroupAdd(merchantNo, bpId, channelCode);
    }


    @Override
    public String getUserIdByMerchantInfo(String merchantNo) {
        return merchantBusinessProductDao.getUserIdByMerchantInfo(merchantNo);
    }


    @Override
    public MerchantBusinessProduct selectByMerchantNo(String merchantNo) {
        return merchantBusinessProductDao.selectByMerchantNo(merchantNo);
    }


    @Override
    public String getDeviceIdByPhone(String mobilephone) {
        return merchantBusinessProductDao.getDeviceIdByPhone(mobilephone);
    }


    @Override
    public List<MerchantBusinessProduct> selectByMerchantNoAll(String merchantNo) {
        return merchantBusinessProductDao.selectByMerchantNoAll(merchantNo);
    }


    public void initSysConfigByKey() {
        Constants.BEHAVIOUR_SERVER_PROJECT = merchantBusinessProductDao.getSysConfigByKey("SENSORS_PROJECT");
        Constants.BEHAVIOUR_SERVER_URL = merchantBusinessProductDao.getSysConfigByKey("SENSORS_URL");
        Constants.BEHAVIOUR_SERVER_TOKEN = merchantBusinessProductDao.getSysConfigByKey("SENSORS_TOKEN");
    }

    public void SendHttpSc(Map<String, Object> oneMap) {

        String valueByKey = sysDictDao.getValueByKey("SENSORS_STATUS");

        if ("0".equals(valueByKey)) {
            return;
        }

        try {
            //JSON BASE64 URLEncoder
            JSONObject jsonObject = new JSONObject(oneMap);
            String encode = Base64.encode(jsonObject.toString().getBytes());
            String data = URLEncoder.encode(encode, "UTF-8");
            String paramSC = "data=" + data + "&zip=0";
            //String sendPost = HttpUtils.sendPost("https://shenceapi.sqianbao.cn/sa?project=default&token=saf7c5e114", paramSC, "UTF-8");
            HttpUtils.sendPost(Constants.BEHAVIOUR_SERVER_URL + "?project=" + Constants.BEHAVIOUR_SERVER_PROJECT + "&token=" + Constants.BEHAVIOUR_SERVER_TOKEN, paramSC, "UTF-8");
            log.info("神策发送数据=========>" + jsonObject);
        } catch (Exception e) {
            log.error("神策出错------", e);
        }
    }


    @Override
    public void sendHttpSynMerAddress(String merNo) {
        try {
            //异常或者失败也能继续往下走。运营单独去处理这个集群切换
            ClientInterface.synMerAddress(getCoreUrl(), merNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getMerCreditCard(String merNo) {
        Map<String, Object> map = merchantBusinessProductDao.getMerCreditCard(merNo);
        if (map != null) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<MerchantBusinessProduct> getByMerAndBpId(String merchantNo) {
        return merchantBusinessProductDao.getByMerAndBpId(merchantNo);
    }


    @Override
    public String getByCodeAndType(String autoMbpChannel, int routeType) {
        return merchantBusinessProductDao.findByCodeAndType(autoMbpChannel, routeType);
    }

    @Override
    public List<MerchantBusinessProduct> selectNextMbpInfo(MerchantBusinessProduct info) {
        return merchantBusinessProductDao.selectNextMbpInfo(info);
    }

    @Override
    public MerchantBusinessProduct getMerchantBusinessProductInfo(Long mbpId) {
        return merchantBusinessProductDao.getMerchantBusinessProductInfo(mbpId);
    }

    @Override
    public MerchantBusinessProduct getMbpInfoDetail(Long mbpId) {
        return merchantBusinessProductDao.getMbpInfoDetail(mbpId);
    }


    @Override
    public List<Map> queryHuoTiChannels() throws Exception {
        return merchantBusinessProductDao.queryHuoTiChannels();
    }


    @Override
    public List<Map<String, String>> queryAuthChannels() {
        return sysDictDao.getListByKey("authChannel");
    }

    @Override
    public List<String> selectBpIdsByMerchantNo(String merchantNo) {
        return merchantBusinessProductDao.selectBpIdsByMerchantNo(merchantNo);
    }

    @Override
    public MerchantBusinessProduct selectByMerchantNoAndBpId(String merchantNo, String bpId) {
        return merchantBusinessProductDao.selectByMerchantNoAndBpId(merchantNo, bpId);
    }

    @Override
    public List<Map<String, Object>> selectTerBpInfo(String merchantNo, String bpId) {
        return merchantBusinessProductDao.selectTerBpInfo(merchantNo, bpId);
    }

    @Override
    public int updateMerchantBusinessProduct(String merchantNo, String oldBpId, String newBpId) {
        return merchantBusinessProductDao.updateMerchantBusinessProduct(merchantNo, oldBpId, newBpId);
    }

    @Override
    public int insertMerBusProHis(MerchantBusinessProductHistory hisMbp) {
        return merchantBusinessProductDao.insertMerBusProHis(hisMbp);
    }

    @Override
    public int deleteMerBusItem(String merchantNo) {
        return merchantBusinessProductDao.deleteMerBusItem(merchantNo);
    }

    @Override
    public List<ServiceInfoBean> listServiceInfoByBpId(String bpId) {
        return merchantBusinessProductDao.listServiceInfoByBpId(bpId);
    }

    @Override
    public int deleteMerRate(String bpId, String merchantNo) {
        return merchantBusinessProductDao.deleteMerRate(bpId, merchantNo);
    }

    @Override
    public int deleteMerQuota(String bpId, String merchantNo) {
        return merchantBusinessProductDao.deleteMerQuota(bpId, merchantNo);
    }

    @Override
    public int updateMerchantService(String merchantNo, String oldBpId, String newBpId, String oldServiceId, String newServiceId) {
        return merchantBusinessProductDao.updateMerchantService(merchantNo, oldBpId, newBpId, oldServiceId, newServiceId);
    }

    @Override
    public void bacthInsertServiceRate(List<ServiceRate> newServiceRateList, String merchantNo) {
        merchantBusinessProductDao.bacthInsertServiceRate(newServiceRateList, merchantNo);
    }

    @Override
    public int bacthInsertServiceQuota(List<ServiceQuota> newServiceQuotaList, String merchantNo) {
        return merchantBusinessProductDao.bacthInsertServiceQuota(newServiceQuotaList, merchantNo);
    }

    @Override
    public int countZF_ZQAndSyncSuccess(String merchantNo, String bpId) {
        return merchantBusinessProductDao.countZF_ZQAndSyncSuccess(merchantNo, bpId);
    }

    /**
     * 更换业务产品流程
     */
    @Override
    public boolean replaceBusinessProduct(final String merchantNo, String oldBpId, final String newBpId, String remark, Long mbpId) {
        log.info(String.format(
                "replaceBusinessProduct: merchantNo-> %s, oldBpId -> %s, newBpId -> %s",
                merchantNo, oldBpId, newBpId));
        MerchantInfo merInfo = merchantInfoService.selectByMerNo(merchantNo);
        if (merchantBusinessProductDao.updateMerBusTer(newBpId, merchantNo, oldBpId) < 1) {
            throw new BossBaseException("更新机具信息失败");
        }
        if (merchantBusinessProductDao.updateMerchantBusinessProduct(merchantNo, oldBpId, newBpId) != 1) {
            throw new BossBaseException("更新业务产品失败");
        }

        MerchantBusinessProductHistory mbpHis = new MerchantBusinessProductHistory();
        mbpHis.setSourceBpId(oldBpId);
        mbpHis.setNewBpId(newBpId);
        mbpHis.setOperationType("2");// 2更换
        mbpHis.setOperationPersonType("1");//1运营2商户3代理商
        mbpHis.setCreateTime(new Date());
        mbpHis.setOperationPersonNo(StringUtil.filterNull(CommonUtil.getLoginUser().getId()));// 操作人编号
        mbpHis.setMerchantNo(merchantNo);
        mbpHis.setRemark(remark);
        mbpHis.setMbpId(mbpId);
        if (merchantBusinessProductDao.insertMerBusProHis(mbpHis) != 1) {
            throw new BossBaseException("写入商户业务产品历史表失败");
        }
        // 删除审核失败进件项
        merchantBusinessProductDao.deleteMerBusItem(merchantNo);
        updateMerchantService(merInfo, oldBpId, newBpId);
        // 如果是再中付同步过,且同步成功的话,这需要条用中付的修改接口(修改费率)
        if (merchantBusinessProductDao.countZF_ZQAndSyncSuccess(merchantNo, newBpId) >= 1) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(500);
                        SysDict sysDict = sysDictDao.getByKey("ZFZQ_ACCESS_URL");
                        String accessUrl = sysDict.getSysValue() + "zfMerchant/zfMerUpdate";
                        Map<String, Object> marMap = new HashMap<String, Object>();

                        marMap.put("merchantNo", merchantNo);
                        marMap.put("bpId", newBpId);
                        marMap.put("operator", StringUtil.filterNull(CommonUtil.getLoginUser().getId()));
                        marMap.put("changeSettleCard", "0");
                        List<String> channelList = new ArrayList<>();
                        channelList.add("ZF_ZQ");
                        marMap.put("channelCode", channelList);
                        String paramStr = JSON.toJSONString(marMap);
                        log.info("ZFZQ_ACCESS_URL:" + accessUrl + "\n paramStr:" + paramStr);
                        String result = new ClientInterface(accessUrl, null).postRequestBody(paramStr);
                        log.error("调用上游同步返回数据:" + result);
                    } catch (Exception e) {
                        log.error("调用上游同步费率接口失败", e);
                    }
                }
            }).start();
        }
        return true;
    }

    @Override
    public List<BusinessProductDefine> selectGroupBpInfo(String merchantNo, String bpId) {
        MerchantInfo merchantInfo = merchantInfoDao.selectByMerNo(merchantNo);
        if(merchantInfo == null) {
            throw new BossBaseException("商户不存在");
        }
        return merchantBusinessProductDao.selectGroupBpInfo(merchantInfo.getAgentNo(), bpId);
    }

    @Override
    public List<MerchantBusinessProductHistory> selectMerchantBusinessProductHistoryList(Long mbpId, String merchantNo) {
        //因为mbpId是后加字段，存量数据都是空的，所以索引加在merchantNo会比较好
        List<MerchantBusinessProductHistory> list = merchantBusinessProductDao.selectMerchantBusinessProductHistoryList(mbpId, merchantNo);
        if(list == null || list.size() < 1) {
            return null;
        }
        for(MerchantBusinessProductHistory item: list) {
            //业务产品名称
            item.setSourceBpName(getBusinessProductDefineName(item.getSourceBpId()));
            item.setNewBpName(getBusinessProductDefineName(item.getNewBpId()));
            //操作人名称

            item.setOperationPerson(getOperationPerson(item));
        }
        return list;
    }

    @Override
    public List<ZjxMerSwitchLog> selectZjxMerSwitchLogList(String merchantNo) {
        return merchantBusinessProductDao.selectZjxMerSwitchLogList(merchantNo);
    }

    /**
     * 根据操作人类型，获取操作人名称
     * @param item
     * @return
     */
    private String getOperationPerson(MerchantBusinessProductHistory item) {
        String operationPerson = "";
        //操作人类型，1运营2商户3代理商
        switch (item.getOperationPersonType()) {
            case "1":
                UserInfo userInfo = userDao.getUserInfoById(Integer.valueOf(item.getOperationPersonNo()));
                if(userInfo != null) {
                    operationPerson = userInfo.getRealName();
                }
                break;
            case "2":
                MerchantInfo merchantInfo = merchantInfoDao.selectByMerNo(item.getOperationPersonNo());
                if(merchantInfo != null) {
                    operationPerson = merchantInfo.getMerchantName();
                }
                break;
            case "3":
                AgentInfo agentInfo = agentInfoDao.select(item.getOperationPersonNo());
                if(agentInfo != null) {
                    operationPerson = agentInfo.getAgentName();
                }
                break;
        }
        return operationPerson;
    }

    /**
     * 获取业务产品名称
     * @param bpId
     * @return
     */
    private String getBusinessProductDefineName(String bpId) {
        BusinessProductDefine sourceBpd = businessProductInfoDao.getProductBase(Integer.valueOf(bpId));
        if(sourceBpd != null) {
            return sourceBpd.getBpName();
        }
        return null;
    }

    private void updateMerchantService(MerchantInfo merchantInfo, String oldBpId, String newBpId) {

        List<ServiceInfoBean> oldServiceInfoList = merchantBusinessProductDao.listServiceInfoByBpId(oldBpId);
        log.info("oldServiceInfoList -> " + oldServiceInfoList);
        List<ServiceInfoBean> newServiceInfoList = merchantBusinessProductDao.listServiceInfoByBpId(newBpId);
        log.info("newServiceInfoList -> " + newServiceInfoList);
        if (oldServiceInfoList == null || newServiceInfoList == null) {
            throw new BossBaseException("没有找到相应的服务信息");
        }
        if (oldServiceInfoList.size() != newServiceInfoList.size()) {
            throw new BossBaseException("新旧业务产品的服务信息不一一对应.");
        }
        Map<String, ServiceInfoBean> oldServiceInfoMap = translateServiceId2MapKey(oldServiceInfoList);
        Map<String, ServiceInfoBean> newServiceInfoMap = translateServiceType2MapKey(newServiceInfoList);
        log.info("oldServiceInfoMap -> " + oldServiceInfoMap);
        log.info("newServiceInfoMap -> " + newServiceInfoMap);

        String oneAgentNo = merchantInfo.getOneAgentNo();
        String merchantNo = merchantInfo.getMerchantNo();

        // 删除商户服务费率
        merchantBusinessProductDao.deleteMerRate(oldBpId, merchantNo);
        // 删除商户服务限额
        merchantBusinessProductDao.deleteMerQuota(oldBpId, merchantNo);

        for (Map.Entry<String, ServiceInfoBean> oldServiceEntry : oldServiceInfoMap.entrySet()) {
            String oldServiceId = oldServiceEntry.getKey();
            String oldServiceType = oldServiceEntry.getValue().getServiceType();
            ServiceInfoBean newServiceInfoBean = newServiceInfoMap.get(oldServiceType);
            if (newServiceInfoBean == null || StringUtils.isBlank(newServiceInfoBean.getServiceId())) {
                throw new BossBaseException("旧业务产品的的服务(" + oldServiceId + ")没找到对应的新服务");
            }
            String newServiceId = newServiceInfoBean.getServiceId();
            if (merchantBusinessProductDao.updateMerchantService(merchantNo, oldBpId, newBpId, oldServiceId,
                    newServiceId) != 1) {
                throw new BossBaseException("更新商户服务失败");
            }
            if (StringUtils.equals("0", newServiceInfoBean.getFixedRate())) {
                List<ServiceRate> newServiceRateList = merchantInfoDao.getServiceRateByServiceId(oneAgentNo,
                        newServiceId);
                if (newServiceRateList != null && !newServiceRateList.isEmpty()) {
                    merchantBusinessProductDao.bacthInsertServiceRate(newServiceRateList, merchantNo);
                }
            }
            if (StringUtils.equals("0", newServiceInfoBean.getFixedQuota())) {
                List<ServiceQuota> newServiceQuotaList = merchantInfoDao.getServiceQuotaByServiceId(oneAgentNo,
                        newServiceId);
                if (newServiceQuotaList != null && !newServiceQuotaList.isEmpty()) {
                    merchantBusinessProductDao.bacthInsertServiceQuota(newServiceQuotaList, merchantNo);
                }
            }
        }
    }

    /**
     * 把list转化为map,key 为 服务类型(serviceId)
     *
     * @param serviceInfoList
     * @return
     */
    private Map<String, ServiceInfoBean> translateServiceId2MapKey(List<ServiceInfoBean> serviceInfoList) {
        Map<String, ServiceInfoBean> resultMap = new HashMap<>();
        for (ServiceInfoBean bean : serviceInfoList) {
            resultMap.put(bean.getServiceId(), bean);
        }
        return resultMap;
    }

    /**
     * 把list转化为map,key 为 服务id(seriviceType)
     *
     * @param serviceInfoList
     * @return
     */
    private Map<String, ServiceInfoBean> translateServiceType2MapKey(List<ServiceInfoBean> serviceInfoList) {
        Map<String, ServiceInfoBean> resultMap = new HashMap<>();
        for (ServiceInfoBean bean : serviceInfoList) {
            resultMap.put(bean.getServiceType(), bean);
        }
        return resultMap;
    }
}
